import React from 'react';
import { View, Text, TouchableOpacity, Platform } from 'react-native';
import _ from 'lodash';
import { Actions } from 'react-native-router-flux';
import styles from './styles.js';

export default props => {
  const {
    cancelTitle = '取消',
    okTitle = '提交',
    onOk,
    onCancel,
    title = '表单',
    titleStyle,
    disabled,
  } = props;
  const cancelHandle = () => {
    if (_.isFunction(onCancel)) {
      onCancel();
    } else if (onCancel) {
      Actions.pop();
      // Actions.drawerClose();
    }
  };
  const okHandle = () => {
    if (_.isFunction(onOk)) {
      onOk();
    }
  };
  return (
    <View style={[titleStyle, styles.wrap]}>
      {onCancel ? (
        <TouchableOpacity onPress={cancelHandle}>
          <Text style={[styles.left]}>{cancelTitle}</Text>
        </TouchableOpacity>
      ) : null}
      <Text style={[styles.center]}>{title}</Text>
      {disabled ? <Text style={[styles.right]}>提交中...</Text> : <TouchableOpacity onPress={okHandle}>
        <Text style={[styles.right]}>{okTitle}</Text>
      </TouchableOpacity>}
    </View>
  );
};
export const errorHandle = err =>
  _.flatten(
    _.map(_.values(err), ({ errors }) =>
      _.map(errors, ({ message }) => message)
    )
  );
